//
//  LoginAlertView.m
//  NJUSTHelper
//
//  Created by 郑越辉 on 2017/9/15.
//  Copyright © 2017年 njust. All rights reserved.
//

#import "LoginAlertView.h"
#import "Define.h"

#define POPVIEW_WIDTH   300
#define POPVIEW_HEIGTH  150
#define BTN_HEIGTH      48


@interface LoginAlertView()
@property (nonatomic, strong) UIView *backgroundview;
@property (nonatomic, strong) UIView *popview;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *okButton;

@end

@implementation LoginAlertView

-(instancetype)init {
    if(self == [super init]) {
        self.frame = [UIScreen mainScreen].bounds ;
        self.backgroundview = [[UIView alloc]initWithFrame:self.bounds];
        self.backgroundview.backgroundColor = HexAColor(@"#000000", 0.5);
        [self addSubview:self.backgroundview];
        
        ///弹窗
        self.popview = [[UIView alloc]init];
        self.popview.backgroundColor = [UIColor whiteColor];
        self.popview.layer.cornerRadius = 10;
        self.popview.layer.masksToBounds = YES;
        [self.backgroundview addSubview:self.popview];
        self.popview.sd_layout
        .centerXEqualToView(self)
        .centerYEqualToView(self)
        .widthIs(POPVIEW_WIDTH)
        .heightIs(POPVIEW_HEIGTH);
        
        ///提示信息
        self.messageLabel = [[UILabel alloc]init];
        self.messageLabel.font = [UIFont systemFontOfSize:18];
        self.messageLabel.text = @"您尚未登录,是否前往登录?";
        self.messageLabel.textColor = DEFAULT_FONT_COLOR;
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        [self.popview addSubview:self.messageLabel];
        self.messageLabel.sd_layout
        .centerXEqualToView(self.popview)
        .topSpaceToView(self.popview, 35)
        .leftSpaceToView(self.popview, 12)
        .rightSpaceToView(self.popview, 12)
        .heightIs(30);
        
        
        //cancel btn
        self.cancelButton = [[UIButton alloc] init];
        [self.popview addSubview:self.cancelButton];
        [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [self.cancelButton setTitleColor:DEFAULT_FONT_COLOR forState:UIControlStateNormal];
        self.cancelButton.layer.borderColor = [Default_BorderColor CGColor];
        self.cancelButton.layer.borderWidth = 0.5;
        [self.cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        self.cancelButton.sd_layout
        .leftEqualToView(self.popview)
        .bottomEqualToView(self.popview)
        .widthIs(self.popview.width*0.5)
        .heightIs(BTN_HEIGTH);
        
        //cancel btn
        self.okButton = [[UIButton alloc] init];
        [self.popview addSubview:self.okButton];
        [self.okButton setTitle:@"去登录" forState:UIControlStateNormal];
        [self.okButton setTitleColor:DEFAULT_FONT_COLOR forState:UIControlStateNormal];
        self.okButton.layer.borderColor = [Default_BorderColor CGColor];
        self.okButton.layer.borderWidth = 0.5;
        [self.okButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        self.okButton.sd_layout
        .rightEqualToView(self.popview)
        .bottomEqualToView(self.popview)
        .widthIs(self.popview.width*0.5)
        .heightIs(BTN_HEIGTH);
        
    }
    return self;
}

#pragma mark  动画效果
- (void)creatShowAnimation
{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.3;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    [self.popview.layer addAnimation:animation forKey:nil];
    
}

- (void)creatRemoveAnimation{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.3;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1.0)]];
    
    animation.values = values;
    
    [self.popview.layer addAnimation:animation forKey:nil];
    //    [self shutDownView];
    [self performSelector:@selector(shutDownView) withObject:nil afterDelay:0.2];
}

-(void)shutDownView {
    
    [self removeFromSuperview];
}

#pragma mark  点击时间
-(void)cancelAction:(UIButton *)sender{
     [self creatRemoveAnimation];
}
-(void)loginAction:(UIButton *)sender{
    if(self.okActionBlock) {
        self.okActionBlock();
    }
    [self creatRemoveAnimation];
}

#pragma mark  对外接口
-(void)show{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    
    
    [self creatShowAnimation];
}

@end
